<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >添加语录
        </el-button>
      </el-col>
    </el-row>
    <el-table border stripev-loading="loading" :data="list">
      <el-table-column label="语录编号" align="center" prop="id"/>
      <el-table-column label="语录" align="center" prop="quotation"/>
      <el-table-column label="添加时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listQuotation, delQuotation, addQuotation} from "@/api/quotation";

export default {
  name: "quotation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 公告表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAdd() {
      this.$prompt('新增语录', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputValidator: value => {
          if (value.length < 1) {
            return "语录不能为空";
          }
        },
        inputPlaceholder: "请输入语录",
        inputErrorMessage: "语录不能为空",
      }).then(val => {
        if (val.action === "confirm") {
          addQuotation({quotation: val.value}).then(res => {
            this.getList();
            this.$modal.msgSuccess("新增成功");
          })
        }
      })
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listQuotation(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.id
      this.$modal.confirm('是否确认删除编号为"' + noticeIds + '"的语录？').then(function () {
        return delQuotation(noticeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    }
  }
};
</script>
